package com.ljj.generator;

import java.util.ArrayList;
import java.util.List;
import java.util.Scanner;

import com.baomidou.mybatisplus.core.exceptions.MybatisPlusException;
import com.baomidou.mybatisplus.core.toolkit.StringPool;
import com.baomidou.mybatisplus.generator.AutoGenerator;
import com.baomidou.mybatisplus.generator.InjectionConfig;
import com.baomidou.mybatisplus.generator.config.DataSourceConfig;
import com.baomidou.mybatisplus.generator.config.FileOutConfig;
import com.baomidou.mybatisplus.generator.config.GlobalConfig;
import com.baomidou.mybatisplus.generator.config.PackageConfig;
import com.baomidou.mybatisplus.generator.config.StrategyConfig;
import com.baomidou.mybatisplus.generator.config.TemplateConfig;
import com.baomidou.mybatisplus.generator.config.po.TableInfo;
import com.baomidou.mybatisplus.generator.config.rules.NamingStrategy;
import com.baomidou.mybatisplus.generator.engine.FreemarkerTemplateEngine;

import org.springframework.util.StringUtils;

public class CodeGenerator {
	/**
	 * <p>
	 * 璇诲彇鎺у埗鍙板唴瀹�
	 * </p>
	 */
	public static String scanner(String tip) {
		Scanner scanner = new Scanner(System.in);
		StringBuilder help = new StringBuilder();
		help.append("璇疯緭鍏�" + tip + "锛�");
		System.out.println(help.toString());
		if (scanner.hasNext()) {
			String ipt = scanner.next();
			if (!StringUtils.isEmpty(ipt)) {
				return ipt;
			}
		}
		throw new MybatisPlusException("璇疯緭鍏ユ纭殑" + tip + "锛�");
	}

	public static void main(String[] args) {
		// 浠ｇ爜鐢熸垚鍣�
		AutoGenerator mpg = new AutoGenerator();

		// 鍏ㄥ眬閰嶇疆
		GlobalConfig gc = new GlobalConfig();
		String projectPath = System.getProperty("user.dir");
		gc.setOutputDir(projectPath + "/src/main/java");
		gc.setAuthor("liujiju");
		gc.setOpen(false);
		gc.setSwagger2(true); // 瀹炰綋灞炴�� Swagger2 娉ㄨВ
		mpg.setGlobalConfig(gc);

		// 鏁版嵁婧愰厤缃�!!!!!!!!!
		DataSourceConfig dsc = new DataSourceConfig();
		dsc.setUrl("jdbc:mysql://localhost:3306/aini?useUnicode=true&useSSL=false&characterEncoding=utf8");
		// dsc.setSchemaName("public");
		dsc.setDriverName("com.mysql.jdbc.Driver");
		dsc.setUsername("root");
		dsc.setPassword("admin");
		mpg.setDataSource(dsc);

		// 鍖呴厤缃�!!!!!!!!!!!
		PackageConfig pc = new PackageConfig();
		// pc.setModuleName(scanner("妯″潡鍚�"));
		pc.setParent("com.ljj");
		mpg.setPackageInfo(pc);

		// 鑷畾涔夐厤缃�
		InjectionConfig cfg = new InjectionConfig() {
			@Override
			public void initMap() {
				// to do nothing
			}
		};

		// 濡傛灉妯℃澘寮曟搸鏄� freemarker
		String templatePath = "/templates/mapper.xml.ftl";
		// 濡傛灉妯℃澘寮曟搸鏄� velocity
		// String templatePath = "/templates/mapper.xml.vm";

		// 鑷畾涔夎緭鍑洪厤缃�
		List<FileOutConfig> focList = new ArrayList<>();
		// 鑷畾涔夐厤缃細琚紭鍏堣緭鍑�
		focList.add(new FileOutConfig(templatePath) {
			@Override
			public String outputFile(TableInfo tableInfo) {
				// 鑷畾涔夎緭鍑烘枃浠跺悕 锛� 濡傛灉浣� Entity 璁剧疆浜嗗墠鍚庣紑銆佹澶勬敞鎰� xml 鐨勫悕绉颁細璺熺潃鍙戠敓鍙樺寲锛侊紒
				return projectPath + "/src/main/resources/mapper/" + "/" + tableInfo.getEntityName() + "Mapper"
						+ StringPool.DOT_XML;
			}
		});
		/*
		 * cfg.setFileCreate(new IFileCreate() {
		 * 
		 * @Override public boolean isCreate(ConfigBuilder configBuilder, FileType
		 * fileType, String filePath) { // 鍒ゆ柇鑷畾涔夋枃浠跺す鏄惁闇�瑕佸垱寤�
		 * checkDir("璋冪敤榛樿鏂规硶鍒涘缓鐨勭洰褰曪紝鑷畾涔夌洰褰曠敤"); if (fileType == FileType.MAPPER) { // 宸茬粡鐢熸垚
		 * mapper 鏂囦欢鍒ゆ柇瀛樺湪锛屼笉鎯抽噸鏂扮敓鎴愯繑鍥� false return !new File(filePath).exists(); } //
		 * 鍏佽鐢熸垚妯℃澘鏂囦欢 return true; } });
		 */
		cfg.setFileOutConfigList(focList);
		mpg.setCfg(cfg);

		// 閰嶇疆妯℃澘
		TemplateConfig templateConfig = new TemplateConfig();

		// 閰嶇疆鑷畾涔夎緭鍑烘ā鏉�
		// 鎸囧畾鑷畾涔夋ā鏉胯矾寰勶紝娉ㄦ剰涓嶈甯︿笂.ftl/.vm, 浼氭牴鎹娇鐢ㄧ殑妯℃澘寮曟搸鑷姩璇嗗埆
		// templateConfig.setEntity("templates/entity2.java");
		// templateConfig.setService();
		// templateConfig.setController();

		templateConfig.setXml(null);
		mpg.setTemplate(templateConfig);

		// 绛栫暐閰嶇疆
		StrategyConfig strategy = new StrategyConfig();
		strategy.setNaming(NamingStrategy.underline_to_camel);
		strategy.setColumnNaming(NamingStrategy.underline_to_camel);
		// strategy.setSuperEntityClass("浣犺嚜宸辩殑鐖剁被瀹炰綋,娌℃湁灏变笉鐢ㄨ缃�!");
		strategy.setEntityLombokModel(true);
		strategy.setRestControllerStyle(true);
		// 鍏叡鐖剁被
		// strategy.setSuperControllerClass("浣犺嚜宸辩殑鐖剁被鎺у埗鍣�,娌℃湁灏变笉鐢ㄨ缃�!");
		// 鍐欎簬鐖剁被涓殑鍏叡瀛楁
		strategy.setSuperEntityColumns("id");
		strategy.setInclude(scanner("琛ㄥ悕锛屽涓嫳鏂囬�楀彿鍒嗗壊").split(","));
		strategy.setControllerMappingHyphenStyle(true);
		strategy.setTablePrefix(pc.getModuleName() + "_");
		mpg.setStrategy(strategy);
		mpg.setTemplateEngine(new FreemarkerTemplateEngine());
		mpg.execute();
	}

}
